<?php

require_once("../common/conn.php");
require_once("../common/common.php");

if (isset($_GET['method'])) {
    $method = trim($_GET['method']);
    if ($method == "get") {
        $sql = "SELECT * FROM t_hibaby_server";
        $db = new DBC();
        $data = $db->fetchData($sql);
        echo json_encode($data);
    } else if ($method == "set") {
        $data = $_GET['data'];
        $array = json_decode($data, true);
        $return = false;
        if (checkArrayNotEmpty($array)) {
            $db = new DBC();
            $sql = "DELETE FROM t_hibaby_server";
            $return = $db->execute($sql);
            foreach ($array as $row) {
                $sql = "INSERT INTO t_hibaby_server(ip,port,name,status,maxUser) VALUES('" . $row['ip'] . "','" . $row['port'] . "','" . $row['name'] . "','" . $row['status'] . "','" . $row['maxUser'] . "')";
                if (!$db->execute($sql)) {
                    $return = false;
                }
            }
        }
        echo json_encode($return);
    } else if ($method == "changeStatus") {
        $ip = $_GET['ip'];
        $status = $_GET['status'];
        $db = new DBC();
        if ($status == "start") {
            $status = "1";
        } else if ($status == "stop") {
            $status = "0";
        }
        $sql = "UPDATE t_hibaby_server SET status = '" . $status . "' WHERE ip = '" . $ip . "'";
        echo $db->execute($sql);
    }
}
?>
